<!--
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-03-23 14:47:56
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-04-11 10:24:09
 * @FilePath: /free-learning-front/src/components/BackToTop.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div 
    class="back-to-top" 
    :class="{ 'visible': showButton }" 
    @click="scrollToTop"
    aria-label="返回顶部"
  >
    <i class="fas fa-chevron-up"></i>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';

// 控制按钮可见性
const showButton = ref(false);

// 滚动到顶部
const scrollToTop = () => {
  window.scrollTo({
    top: 0,
    behavior: 'smooth'
  });
};

// 处理滚动事件，显示/隐藏按钮
const handleScroll = () => {
  const scrollTop = window.scrollY || document.documentElement.scrollTop;
  showButton.value = scrollTop > 300;
};

onMounted(() => {
  window.addEventListener('scroll', handleScroll);
});

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll);
});
</script>

<style scoped>
.back-to-top {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 45px;
  height: 45px;
  background-color: #007bff;
  color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px);
  transition: all 0.3s ease;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  z-index: 900;
}

.back-to-top.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.back-to-top:hover {
  background-color: #0069d9;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
  transform: translateY(-3px);
}

.back-to-top i {
  font-size: 18px;
}

@media (max-width: 768px) {
  .back-to-top {
    width: 40px;
    height: 40px;
    bottom: 20px;
    right: 20px;
  }
  
  .back-to-top i {
    font-size: 16px;
  }
}
</style> 